const upng = require('./upng')
var network = require("../api/api.js")

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const authJudge = (user, pages) => {  
  let url = (pages ? pages : '..') + '/index/index'
  if (user.audit_status === 0) {
    url = (pages ? pages : '..') + '/lo/supply_data'
  } else if (user.audit_status === 1) {
    url = (pages ? pages : '..') + '/audit/audit'
  } else if (user.audit_status === 2 && parseInt(user.test_status) === 0) {
    url = (pages ? pages : '..') + '/index/index'
  } else if (user.audit_status === 2 && parseInt(user.test_status) !== 0) {
    url = (pages ? pages : '..') + '/Success/Success?test_status=' + user.test_status
  } else if (user.audit_status === 3) {
    url = (pages ? pages : '..') + '/Failure/Failure'
  }
  wx.reLaunch({
    url: url
  })
}

const canvasToBase = function (url, size, callback) {
  var that = this
    const platform = wx.getSystemInfoSync().platform
  wx.getImageInfo({
    src: url,
    success: function (info) {
      console.log(this)
      const ctx = wx.createCanvasContext('canvasToBase')
      var ratio = 2;
      var canvasWidth = info.width
      var canvasHeight = info.height;
      while (canvasWidth > (size.width || 200) || canvasHeight > (size.height || 200)) {
        //比例取整
        canvasWidth = Math.trunc(info.width / ratio)
        canvasHeight = Math.trunc(info.height / ratio)
        ratio++;
      }
      that.setData({
        canvasWidth: canvasWidth + 'px',
        canvasHeight: canvasHeight + 'px'
      })
      ctx.drawImage(url, 0, 0, canvasWidth, canvasHeight)
      ctx.draw(false, function () {
        wx.canvasGetImageData({
          canvasId: 'canvasToBase',
          x: 0,
          y: 0,
          width: canvasWidth,
          height: canvasHeight,
          success(resData) {
              if (platform === 'ios') {
                  // 兼容处理：ios获取的图片上下颠倒
                  resData = reverseImgData(resData)
              }
              // 3. png编码
            let pngData = upng.encode([resData.data.buffer], resData.width, resData.height)
            // 4. base64编码
            let base64 = wx.arrayBufferToBase64(pngData)
            that.setData({
              canvasWidth: 0 + 'px',
              canvasHeight: 0 + 'px'
            })
            callback && callback(base64)
          }
        })
      })
    }
  })
}

const reverseImgData = function (res) {
    let w = res.width
    let h = res.height
    let con = 0
    for (let i = 0; i < h / 2; i++) {
        for (let j = 0; j < w * 4; j++) {
            con = res.data[i * w * 4 + j]
            res.data[i * w * 4 + j] = res.data[(h - i - 1) * w * 4 + j]
            res.data[(h - i - 1) * w * 4 + j] = con
        }
    }
    return res
}

const get_paper_info = function (user_id, paper_id, user_paper_id, cb) {
  const that = this
  var params = new Object()
  params.business_param = {}
  params.business_param.user_id = String(user_id)
  params.business_param.paper_id = paper_id
  params.business_param.user_paper_id = user_paper_id
  params.business_param.submit_status = 1
  params.business_param.submit_type = 1
  network.POST({
    params: params,
    url: "get_paper_info",
    success: function (res) {
      const result = res.result[0]
      if (cb) {
        cb(result)
      }
      //拿到解密后的数据，进行代码逻辑
    },
    fail: function () {
      //失败后的逻辑
    },
  })
}

module.exports = {
  formatTime: formatTime,
  authJudge: authJudge,
  canvasToBase: canvasToBase,
  get_paper_info: get_paper_info
}